package com.ruoyi.develop.project.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.develop.goodsCustomer.domain.GoodsCustomer;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.project.domain.Project;
import com.ruoyi.develop.project.service.IProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息管理Controller
 * @author lmm
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/project/project")
public class ProjectController extends BaseController {
    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:query')")
    @GetMapping("/list")
    public TableDataInfo list(Project entity) {
        startPage();
        List<Project> list = projectService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出项目信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:export')")
    @Log(title = "项目信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project entity) {
        List<Project> list = projectService.selectList(entity);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project. class);
        util.exportExcel(response, list, "项目信息管理数据");
    }

    /**
     * 获取项目信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        AjaxResult ajax = AjaxResult.success();
        Project project = projectService.selectById(id);
        ajax.put(AjaxResult.DATA_TAG, project);
        ajax.put("customerIds", project.getCustomers().stream().map(GoodsCustomer::getId).collect(Collectors.toList()));
        ajax.put("salesmanIds", project.getSalesmen().stream().map(SysUser::getUserId).collect(Collectors.toList()));
        return AjaxResult.success(ajax);
    }

    /**
     * 新增项目信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:add')")
    @Log(title = "项目信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Project entity) {
        return toAjax(projectService.insert(entity));
    }

    /**
     * 修改项目信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:edit')")
    @Log(title = "项目信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Project entity) {
        return toAjax(projectService.update(entity));
    }

    /**
     * 删除项目信息管理
     */
    @PreAuthorize("@ss.hasPermi('develop:Project:remove')")
    @Log(title = "项目信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(projectService.deleteByIds(ids));
    }

    /**
     * 检查项目号是否存在
     */
    @GetMapping(value = "/checkCodeUnique/{code}")
    public AjaxResult checkCodeUnique(@PathVariable("code") String code) {
        if (projectService.checkExistByCode(code)){
            return AjaxResult.success(true);
        }
            return AjaxResult.success(false);
    }



}
